package KK.net
{ 
	import KK.Queue.IQueue;
	import KK.Queue.IQueueObject;
	import KK.System.ArgumenError;
	
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.NetConnection;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	public class Q_AMFLoader extends NetConnection implements IQueueObject
	{ 
		
		protected var _queue:IQueue;
		protected var status:int;//0是完成，1是开始，2是等待,3是取消
		
		
		protected var _url:String;
		protected var _path:String;
		
		
		
		protected var _Complete:Function;
		protected var _IOError:Function;
		protected var _SecurityError:Function;
		
		protected var _params:Array;
		public function Q_AMFLoader()
		{
			//TODO: implement function
			super();
			init();
		}
		private function init():void{
			this.objectEncoding=ObjectEncoding.AMF3;
			this.addEventListener(NetStatusEvent.NET_STATUS,NetStatus);
			this.addEventListener(SecurityErrorEvent.SECURITY_ERROR,SecurityError);
		}
		protected function NetStatus(event:NetStatusEvent):void
		{
			switch(event.info.code){
				case "NetConnection.Call.BadVersion":
				case "NetConnection.Connect.Failed":
					
					ArgumenError.checkArgumen(event.info.code);
					IOError(event);
					break; 
			}
		}
		
		protected function IOError(event:Event):void{
			
			if(true==(this._IOError))
			{
				this._IOError(event);
			}else{ 
				LogError(event.type);
			}
		}
		
		protected function SecurityError(event:Event):void
		{
			this._SecurityError(event);
		}
		public function SetLoader(url:String,_path:String,Complete:Function,IOError:Function=null,SecurityError:Function=null,...params):void
		{
			this._url=url;
			this._path=_path;
			
			this._Complete=Complete;
			
			this._params=params;
			
			this.status=2;
			this._IOError=IOError;
			this._SecurityError=SecurityError;
		}
		
		public function get Status():int{
			var result:int=status;
			return result;
		}


		
		public function Run(_queue:IQueue):void
		{
			
			this._queue=_queue;
			status=1;
			Start();
		}
		protected function Complete():void{
			if(null!=this._queue)
			{
				this._queue.Complete(this);
			}
			Reset();
		}
		public function Start():void
		{
			this.connect(this._url);
			if(this._params!=null&&this._params.length>0)
			{
				this.call.apply(this,[this._path,new Responder(SLoaded,SLoaded)].concat(this._params));	
			}else{
				this.call(this._path,new Responder(SLoaded,SLoaded));
			}
			
		}
		
		protected function SLoaded(...args):void
		{
			if(args.length>0)
			{
				this._Complete.apply(this,args);
			}else{
				this._Complete();
			}
			this.Complete();
			Reset();
		}
		public function Reset():int{
			var result:int;
			this.status=0;
			
			this._Complete=undefined;
			this._url="";
			this._path="";
			this._params=null;
			this._queue=null;
			
			return result;
		}
		
		public function Cancel():int
		{
			var result:int=0;
			this.status=3;
			
			
			return result;
		}
		
	}
}